/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filetransserv;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JList;
import javax.swing.ListModel;
import Model.Statistic;
import xmlreader.CommandInterpreter;

/**
 *
 * @author carlosscipione
 */
public class MessageHandler implements Runnable {

    private Socket socket;
    private ArrayList statistic = new ArrayList();
    private ServerManager Manager;
    private String IP;
    private int modelSize;
    private String[] StatusString;
    private ListModel model;
    private JList Status;

    private CommandInterpreter Interpreter;

    public MessageHandler(Socket socket, String ip, JList status) {
        this.socket = socket;
        this.IP = ip;
        this.Status = status;
        this.Interpreter = new CommandInterpreter(this);
        this.Manager = new ServerManager();

//        this.statistic = Statistic.getStatistic();

        Thread t = new Thread(this);
        t.start();

    }

    public void run() {
        try {

            
            this.Interpreter.readCommand(socket.getInputStream());

            //Hay que implementar todas estas llamadas en el Interpreter
            //y otras nuevas.


//            //upload file
//            if (message.equals("upload")) {
//                Manager = new ServerManager();
//                this.statistic = Manager.download((String) ois.readObject(),
//                        socket, this.statistic, ConnectionHandler.poolSockets,
//                        IP, Status);
//            }
//
//            //reportConnections
//            if (message.equals("reportConnections")) {
//                Manager = new ServerManager();
//                Manager.reportConnections(socket, this.statistic,
//                        ConnectionHandler.poolSockets);
//            }
//
//            //reportMaxConnected
//            if (message.equals("reportMaxConnected")) {
//                Manager = new ServerManager();
//                Manager.reportMaxConnected(socket, ConnectionHandler.poolSockets);
//            }


            //socket.close();

            ActualizarConsola("Nuevamente Escuchando...");
            

        } catch (IOException e) {
            //nothing
        }//catch (ClassNotFoundException e) {
//            //nothing
//        }
    }

    public void ActualizarConsola(String message) {

        model = Status.getModel();

        if (model.getSize() > 100) {

            modelSize = 100;
        }
        else {

            modelSize = model.getSize();
        }

        StatusString = new String[modelSize + 1];

        StatusString[0] = message;

        for (int i = 1; i < StatusString.length; i++) {
            StatusString[i] = model.getElementAt(i - 1).toString();
        }

        Status.setListData(StatusString);
    }
    
    
    
    //Getters y Setters
    public String getIP() {
        return IP;
    }

    public void setIP(String IP) {
        this.IP = IP;
    }

    public ServerManager getManager() {
        return Manager;
    }

    public void setManager(ServerManager Manager) {
        this.Manager = Manager;
    }

    public CommandInterpreter getMessage() {
        return Interpreter;
    }

    public void setMessage(CommandInterpreter Message) {
        this.Interpreter = Message;
    }

    public JList getStatus() {
        return Status;
    }

    public void setStatus(JList Status) {
        this.Status = Status;
    }

    public String[] getStatusString() {
        return StatusString;
    }

    public void setStatusString(String[] StatusString) {
        this.StatusString = StatusString;
    }

    public ListModel getModel() {
        return model;
    }

    public void setModel(ListModel model) {
        this.model = model;
    }

    public int getModelSize() {
        return modelSize;
    }

    public void setModelSize(int modelSize) {
        this.modelSize = modelSize;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public ArrayList getStatistic() {
        return statistic;
    }

    public void setStatistic(ArrayList statistic) {
        this.statistic = statistic;
    }


}

